package com.example.test2.main.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.test2.R;
import com.example.test2.activity.AddFriendActivity;
import com.example.test2.adapter.FriendAdapter;
import com.example.test2.databinding.FragmentFriendsBinding;
import com.example.test2.db.control.UserDbController;
import com.example.test2.db.entity.User;
import com.example.test2.event.FriendEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class FriendFragment extends BaseFragment<FragmentFriendsBinding> implements View.OnClickListener {
    private UserDbController dbUserController;
    List<User> list = new ArrayList<>();
    private FriendAdapter friendAdapter;
    private boolean anew=true;

    @Override
    protected void initData() {
        super.initData();
        EventBus.getDefault().register(this);
        dbUserController = UserDbController.getInstance(getContext());
        list = dbUserController.searchAll();
        inAdapter();
    }

    private void inAdapter() {
        getBinding().rvData.setLayoutManager(new LinearLayoutManager(getActivity()));

        if (friendAdapter == null) {
            friendAdapter = new FriendAdapter(getActivity(), list);
            getBinding().rvData.setAdapter(friendAdapter);
        } else {
            friendAdapter.setList(list);
            friendAdapter.notifyDataSetChanged();
        }
    }
    @Override
    protected void initClick() {
        super.initClick();
        getBinding().tvAttention.setOnClickListener(this);
        getBinding().tvNew.setOnClickListener(this);
        getBinding().ivAdd.setOnClickListener(this);
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_friends;
    }

    @Override
    protected FragmentFriendsBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        return FragmentFriendsBinding.inflate(inflater, parent, false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_attention:
                anew=false;
                getBinding().tvAttention.setTextSize(18);
                getBinding().tvNew.setTextSize(15);
                list = dbUserController.searchByWhere(true);
                inAdapter();
                break;
            case R.id.tv_new:
                anew=true;
                getBinding().tvAttention.setTextSize(15);
                getBinding().tvNew.setTextSize(18);
                list = dbUserController.searchAll();
                inAdapter();
                break;
            case R.id.iv_add:
                startActivity(new Intent(getActivity(), AddFriendActivity.class));
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(FriendEvent event) {
        if (anew){
            list = UserDbController.getInstance(getActivity()).searchAll();
            inAdapter();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
